<?php
namespace app\admin\controller;
use think\Db;
use think\Loader;

class Article extends Base{

    //文章列表
    public function lst(){
        $article = Db::name('article') -> field('a.*, c.catename')->alias('a') -> join('cate c', 'c.id = a.cate_id') -> paginate(2);
        $this -> assign('article', $article);
        return $this -> fetch();
    }

    //新增文章
    public function add(){
        if(request() -> isPost()){
            $data = [
                'title' => input('title'),
                'keywords' => input('keywords'),
                'desc' => input('desc'),
                'cate_id' => input('cate_id'),
                'content' => input('content'),
                'time' =>time()
            ];

            //图片上传
            if($_FILES['pic']['tmp_name']){
                $file = request() -> file('pic');
                $info = $file ->move('../uploads');
                if($info){
                    $data['pic'] = ROOT_PATH.'./uploads/'.date('Ymd').'/'.$info -> getFilename();
                }else{
                    return $this -> error($file -> getError());
                }
            }

            $validate = Loader::validate('Article');
            if($validate -> check($data)){
                $db = Db::name('article') -> insert($data);
                if($db){
                    return $this -> success('添加文章成功！', 'Article/lst');
                }else{
                    return $this -> error('添加文章失败！');
                }
            }else{
                return $this -> error($validate -> getError());
            }
            return;

        }else{
            $cates = Db::name('cate') -> select();
            $this -> assign('cates', $cates);
            return $this -> fetch();

        }
    }

    //修改文章
    public function edit(){
        if(request() -> isPost()){
            $data = [
                'id' => input('id'),
                'title' => input('title'),
                'keywords' => input('keywords'),
                'desc' => input('desc'),
                'cate_id' => input('cate_id'),
                'content' => input('content'),
                'time' =>time()
            ];

            //图片上传
            if($_FILES['pic']['tmp_name']){
                $file = request() -> file('pic');
                $info = $file ->move(ROOT_PATH . 'public' . DS . '/static/uploads');
                if($info){
                    $data['pic'] = '/static/uploads/' .date('Ymd').'/'.$info -> getFilename();
                }else{
                    return $this -> error($file -> getError());
                }
            }

            $validate = Loader::validate('Article');
            if($validate -> check($data)){
                $db = Db::name('article') -> update($data);
                if($db){
                    return $this -> success('修改文章成功！', 'Article/lst');
                }else{
                    return $this -> error('修改文章失败！');
                }
            }else{
                return $this -> error($validate -> getError());
            }
            return;

        }else{
            $arts = Db::name('article') -> where('id', input('id')) -> find();
            $cates = Db::name('cate') -> select();
            $this -> assign('cates', $cates);
            $this -> assign('arts', $arts);
            return $this -> fetch();

        }
    }

    //删除文章
    public function del(){
        if(db('article')->delete(input('id'))){
            return $this -> success('删除文章成功！', 'article/lst');
        }else{
            return $this -> error('删除文章失败！');
        }
    }

}